package com.example.demo.controller;

import com.example.demo.entity.ApiResponse;
import com.example.demo.entity.Books;
import com.example.demo.service.BooksService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BookController {
    private final static Logger logger = LoggerFactory.getLogger(BookController.class);
    @Autowired
    private BooksService booksService;

    @PreAuthorize("hasAuthority('book:read')")
    @GetMapping("/books")
    public ResponseEntity<ApiResponse> getList() {
        List<Books> list;
        ApiResponse apiResponse;
        try {
            list = booksService.getList();
            if (list != null) {
                apiResponse = new ApiResponse(1, "数据获取成功", list);
            } else {
                apiResponse = new ApiResponse(0, "数据不存在", null);
            }
            logger.info("数据:{}", list);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据获取失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('book:read')")
    @GetMapping("/books/{id}")
    public ResponseEntity<ApiResponse> getBook(@PathVariable Integer id) {
        Books books;
        ApiResponse apiResponse;
        try {
            books = booksService.getBook(id);
            if (books != null) {
                apiResponse = new ApiResponse(1, "数据获取成功", books);
            } else {
                apiResponse = new ApiResponse(0, "数据不存在", null);
            }
            logger.info("id:{}，数据:{}", id, books);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据获取失败,id:{}", id);
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据获取失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('book:create')")
    @PostMapping("/books")
    public ResponseEntity<ApiResponse> addBook(@RequestBody Books book) {
        ApiResponse apiResponse;
        try {
            int line = booksService.addBook(book);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据添加成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据添加失败", null);
            }
            logger.info("接收数据:{}", book);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据添加失败:{}", book);
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据添加失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('book:update')")
    @PutMapping("/books/{id}")
    public ResponseEntity<ApiResponse> updateBook(@PathVariable Integer id, @RequestBody Books book) {
        ApiResponse apiResponse;
        try {
            book.setId(id);
            int line = booksService.updateBook(book);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据更新成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据更新失败", null);
            }
            logger.info("id:{}，数据:{}", id, book);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据更新失败,id:{},数据:{}", id, book);
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据更新失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('book:delete')")
    @DeleteMapping("/books/{id}")
    public ResponseEntity<ApiResponse> deleteBook(@PathVariable Integer id) {
        ApiResponse apiResponse;
        try {
            int line = booksService.delBook(id);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据删除成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据删除失败", null);
            }
            logger.info("id:{}", id);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据删除失败,id:{}", id);
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据删除失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('book:read')")
    @GetMapping("/books/serach/{keyword}")
    public ResponseEntity<ApiResponse> searchBook(@PathVariable String keyword) {
        ApiResponse apiResponse;
        try {
            List<Books> books = booksService.searchBook(keyword);
            if (books.size() > 0) {
                apiResponse = new ApiResponse(1, "数据查询成功", books);
            } else {
                apiResponse = new ApiResponse(0, "数据不存在", null);
            }
            logger.info("keyword:{}", keyword);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据查询失败,keyword:{}", keyword);
            logger.error("数据查询失败：{}", e.getMessage());
            apiResponse = new ApiResponse(2, "数据查询失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }
}
